/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-06 15:05:46  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import com.joysuch.wwyt.bp.bean.BpWorkSafetyApplyMethodEditBean;
import com.joysuch.wwyt.bp.entity.BpWorkSafetyApplyMethod;
import com.joysuch.wwyt.bp.enums.SafetyWorkTypes;
import com.joysuch.wwyt.bp.repository.BpWorkSafetyApplyMethodDao;
import com.joysuch.wwyt.bp.service.BpWorkSafetyApplyMethodService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class BpWorkSafetyApplyMethodServiceImpl implements BpWorkSafetyApplyMethodService {

    @Autowired
    private BpWorkSafetyApplyMethodDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BaseUserDao baseUserDao;

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     * 
     * @param id
     *            要删除的业务ID
     * @throws BussinessRestrictionsException
     *             抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {

    }

    @Override
    public BpWorkSafetyApplyMethod findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public List<BpWorkSafetyApplyMethodEditBean> findByCertTypeAndId(SafetyWorkTypes type, Long workId) {
        List<BpWorkSafetyApplyMethod> methods = dao.findByTypeAndWorkId(type.getName(), workId);

        return PageDataUtils.convertListData(methods,
                new PageDataConvert<BpWorkSafetyApplyMethodEditBean, BpWorkSafetyApplyMethod>() {

                    @Override
                    public BpWorkSafetyApplyMethodEditBean convert(BpWorkSafetyApplyMethod f) {
                        BpWorkSafetyApplyMethodEditBean bean = new BpWorkSafetyApplyMethodEditBean();
                        if (f.getConfirmUserId() != null) {
                            bean.setConfirmUserName(baseUserDao.getBaseUserNameById(f.getConfirmUserId()));
                            bean.setConfirmUserId(f.getConfirmUserId());
                        }
                        bean.setMethod(f.getMethod());
                        bean.setId(f.getId());
                        return bean;
                    }

                });
    }

}
